import Vuex from "vuex";
import Vue from "vue";
import { loginStatus, getUserInfo } from "@/apis/apis";

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    detail: {
      code: "",
      is_pull_new: false,
      is_reward: false,
      money: 0,
      short_code: "",
      phone:''
    },
    isLogined: false
  },
  mutations: {
    mutateLoginStatus(state, payload) {
      state.isLogined = payload;
    },
    mutateUserInfo(state, payload) {
      state.detail = payload;
    }
  },
  getters:{
    detail(state){
      return state.detail
    }
  },
  actions: {
    async fetchUserInfo({ commit }) {
      const detail = await getUserInfo();
      // console.log(detail);
      commit("mutateUserInfo", detail);
    },
    async fetchUserLoginStatus({ commit, dispatch }) {
      const login = await loginStatus();
      commit("mutateLoginStatus", login.login);
      if (login.login) dispatch("fetchUserInfo");
    }
  }
});

export default store;
